<?php
namespace app\libraryadmin\controller;
use  think\Controller;
use think\Db;
use think\Session;

class Index extends Controller
{
     public function index()
    {
    	if(request()->isPost())
    	{
    		$name=input("param.adminname");
    		$rs= Db::name("libraryadmin")->where("laname='$name'")->find();
    		if($rs)
    		{
    				
    				if($rs["password"]==md5(input("param.password")))
    				{
    					Session::set('login_laid',$rs["laid"]); 
						Session::set('login_libraryadmin',$rs['laname']); 
						$this->success("登录成功","book/index");
    				}
    				else $this->error("密码错误");
    		}
    		else $this->error("用户名错误");
    	}
        return $this->fetch();
    }
    
    public function findpwd(){
        if(request()->isPost()){
        	$email=input('email');
        	//该邮箱是否存在
        	$emailinfo=Db::name("libraryadmin")->where("email",$email)->count();
        	if($emailinfo){
        		$title = "找回密码通知！";
				$srand = rand ( 11111, 99999 );
				$data ['rand'] = $srand;
				Db::name("libraryadmin")->where ( "email = '$email'" )->update ( $data ); // 更新数据库
				// echo Db::name("users")->getLastSQL();die();
				$content = "校验码:$srand";

				if (SendMail ( $email, $title, $content )) {
					$this->success( "发送成功,请到邮箱查看校验码!", url('setpwd',['email'=>$email]) );
					die ();
				} else {
					$this->error( $mail->ErrorInfo );
					die ();
				}
        	}else{
        		$this->error("邮箱不存在");
        	}
        }
       		else return $this->fetch();
           }
    public function setpwd($email)
    {
    	
    	
    	if(request()->isPost())
    	{

    		$rs=Db::name("libraryadmin")->where("email='$email'")->find();
    		//var_dump($rs);
    		if($rs['rand']==input("param.rand"))
    		{
    			$data["password"]=md5(input("param.newpwd"));
    			$data1["password"]=null;
    			Db::name("libraryadmin")->where("email='$email'")->update($data1);
    			$pwd=Db::name("libraryadmin")->where("email='$email'")->update($data);
    			if($pwd)
    			{
    				$data["rand"]=null;
    				Db::name("libraryadmin")->where("email='$email'")->update($data);
    				$this->success("修改密码成功","index");
    			}
    			//else $this->error("修改密码失败");
    		}
    		//else $this -> error("验证码错误");
    	}
    	else 
    		{
    			return $this->fetch();
  		  	}
    }
    public function repassword()
    {
        if(request()->isPost())
        {
            $old=input("param.old");
            $name=Session::get("login_libraryadmin");
            $rs=Db::name("libraryadmin")->where("laname='$name' and password=md5('$old')")->find();
            if($rs)
            {
                $new1=input("param.new1");
                $new2=input("param.new2");
                if($new1==$new2)
                {
                    $data=['password'=>md5("$new1")];
                    $pwd=Db::name("libraryadmin")->where("laname='$name'")->update($data);
                    if($pwd)
                    {
                        Session::clear();
                        $this->success("修改密码成功",'index');
                    }
                    else $this->error("修改密码失败");
                }
                else $this->error("两次密码不一致");
            }
            else $this->error("原密码错误");
        }
    }
    public function logout()
    {
        Session::clear();
        $this->success("注销成功");
    }
}
